package com.smart.mattress.account.mail;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


/**
 * 邮件发送服务
 * Created by rencong on 16/5/29.
 */
@Component("emailServer")
public class EmailServer {

    Logger logger = LoggerFactory.getLogger(EmailServer.class);
    @Autowired
    JavaMailSender javaMailSender;

    /***
     * 邮件发送服务
     * @param to 收件人
     * @param from  发件人
     * @param subject 主题
     * @param text 邮件内容
     */
    public void send(String to,String from,String subject,String text) {
        MimeMessage mail = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);


            helper.setTo(to);
            //helper.setReplyTo(replyTo);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(text);

        }catch (MessagingException e) {
            //e.printStackTrace();
            logger.error("send email error:",e);
        } finally {
            javaMailSender.send(mail);
        }

        //return helper;
    }

}
